package com.fwlst.module_home

import android.annotation.SuppressLint
import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.activity.enableEdgeToEdge
import androidx.activity.viewModels
import androidx.compose.animation.Crossfade
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.material3.Icon
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.NavigationBar
import androidx.compose.material3.NavigationBarItem
import androidx.compose.material3.NavigationBarItemDefaults
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.unit.dp
import androidx.navigation.NavGraph.Companion.findStartDestination
import androidx.navigation.compose.NavHost
import androidx.navigation.compose.composable
import androidx.navigation.compose.currentBackStackEntryAsState
import androidx.navigation.compose.rememberNavController
import com.alibaba.android.arouter.facade.annotation.Route
import com.fwlst.lib_base.utils.AdUtils
import com.fwlst.lib_base.ui.BannerAdView
import com.fwlst.lib_base.constant.GlobalUserInfo
import com.fwlst.module_home.config.TabConfig
import com.fwlst.module_home.model.HomeTabViewModel


@Route(path = "/home/route/HomeTabActivityBat")
class HomeTabActivityBat : ComponentActivity() {
    private val viewModel: HomeTabViewModel by viewModels()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()
        // 更新用户信息
        GlobalUserInfo.updateUserInfo()
        // 加载插屏广告
        AdUtils.loadNewInteractionAd(this)
        setContent {
            DynamicTabApp(viewModel)
        }
    }
}

@SuppressLint("UnusedContentLambdaTargetStateParameter")
@Composable
fun DynamicTabApp(viewModel: HomeTabViewModel) {
    val tabConfigs by viewModel.tabConfigs.collectAsState()
    val navController = rememberNavController()
    val navBackStackEntry by navController.currentBackStackEntryAsState()

    Scaffold(
        bottomBar = {
            if (tabConfigs.isNotEmpty()) {
                NavigationBar {
                    val currentRoute = navBackStackEntry?.destination?.route
                    tabConfigs.forEach { config ->
                        val selected = currentRoute == config.route
                        NavigationBarItem(
                            icon = {
                                Crossfade(targetState = selected, label = "") { isSelected ->
                                    Icon(
                                        painter = painterResource(
                                            id = if (isSelected) config.selectedIcon
                                            else config.unselectedIcon
                                        ),
                                        contentDescription = config.title,
                                        modifier = Modifier.size(24.dp),
                                        tint = Color.Unspecified
                                    )
                                }
                            },
                            label = {
                                Text(
                                    text = config.title
                                )
                            },
                            selected = selected,
                            onClick = {
                                navController.navigate(config.route) {
                                    // 保持一致的导航行为
                                    popUpTo(navController.graph.findStartDestination().id) {
                                        saveState = true
                                    }
                                    launchSingleTop = true
                                    restoreState = true
                                }
                            },
                            colors = NavigationBarItemDefaults.colors(
                                indicatorColor = MaterialTheme.colorScheme.surfaceContainerHighest
                            )
                        )
                    }
                }
            }
        }
    ) { innerPadding ->
        Column(
            modifier = Modifier
                .padding(innerPadding)
                .fillMaxSize()
        ) {
            Box(
                modifier = Modifier
                    .weight(1f)
                    .fillMaxWidth()
            ) {
                NavHost(
                    navController = navController,
                    startDestination = TabConfig.HOME.route
                ) {
                    tabConfigs.forEach { config ->
                        composable(config.route) {
                            when (config.route) {
                                TabConfig.HOME.route -> TabConfig.HOME.screen()
                                TabConfig.TAB1.route -> TabConfig.TAB1.screen()
                                TabConfig.TAB2.route -> TabConfig.TAB2.screen()
                                TabConfig.PROFILE.route -> TabConfig.PROFILE.screen()
                            }
                        }
                    }
                }
            }
            BannerAdView()
        }


    }
}